<<<<<<< HEAD
//package com.liy.jiagou.gateway.filter;
//
//import com.liy.jiagou.gateway.utils.JWTUtil;
//import org.apache.commons.lang.StringUtils;
//import org.springframework.cloud.gateway.filter.GatewayFilterChain;
//import org.springframework.cloud.gateway.filter.GlobalFilter;
//import org.springframework.core.Ordered;
//import org.springframework.http.server.reactive.ServerHttpRequest;
//import org.springframework.http.server.reactive.ServerHttpResponse;
//import org.springframework.stereotype.Component;
//import org.springframework.web.server.ServerWebExchange;
//import reactor.core.publisher.Mono;
//
////@Component
//public class AuthorizeFilter implements GlobalFilter, Ordered {
//
//
//    @Override
//    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//
//        // 请求
//        ServerHttpRequest serverHttpRequest = exchange.getRequest();
//
//        // 响应
//        ServerHttpResponse serverHttpResponse = exchange.getResponse();
//
//        //
//        String uri = serverHttpRequest.getURI().getPath();
//
//        //  检查白名单（配置） 直接通过
//        if (uri.indexOf("/auth-server/login") >= 0) {
//            return chain.filter(exchange);
//        }
//
//
//        String token = serverHttpRequest.getHeaders().getFirst("Authorization");
//
//        //
//        String[] array = token.split(" ");
//
//        String access_jwt_token = array[1];
//
//
//        if (StringUtils.isBlank(access_jwt_token)) {
//            throw new RuntimeException("access_jwt_token is token wrong");
//        }
//
//        JWTUtil.verifyToken(access_jwt_token, "jwt_secret");
//
//
//        System.out.println( "------------AuthorizeFilter--------------" + access_jwt_token );
//
//        //ServerHttpRequest mutableReq = serverHttpRequest.mutate().header("userId", userId).build();
//
//        // ServerWebExchange mutableExchange = exchange.mutate().request(mutableReq).build();
//
//        return chain.filter(exchange);
//
//
//    }
//
//
//    @Override
//    public int getOrder() {
//        return -100;
//    }
//}
=======
package com.liy.jiagou.gateway.filter;

// import com.liy.jiagou.gateway.utils.JWTUtil;

// import org.apache.commons.lang.StringUtils;


import com.liy.jiagou.gateway.utils.JWTUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.Map;

@Component
public class AuthorizeFilter implements GlobalFilter, Ordered {

    private final Logger log= LoggerFactory.getLogger(AuthorizeFilter.class);


    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {

        // 请求
        ServerHttpRequest serverHttpRequest = exchange.getRequest();

        // 响应
        ServerHttpResponse serverHttpResponse = exchange.getResponse();

        //
        String uri = serverHttpRequest.getURI().getPath();

        //  检查白名单（配置） 直接通过
        if (uri.indexOf("/auth-server/login") >= 0) {
            return chain.filter(exchange);
        }

        // 请求头
        HttpHeaders headers = serverHttpRequest.getHeaders();

//        String token = serverHttpRequest.getHeaders().getFirst("Authorization");

        Map<String, String> stringStringMap = headers.toSingleValueMap();
        stringStringMap.forEach( (key,value) -> {
            log.error("-----------------  key --------------->   " +  value);
        });



        //
        String bearer_token = headers.toSingleValueMap().get("Authorization");
        if (StringUtils.isEmpty(bearer_token)) {
            throw new RuntimeException("access_jwt_token is token wrong !!!!  no token liy");
        }


        String[] array = bearer_token.split(" ");

        String token_no_bearer = array[1];


        log.error("-----------------  token_no_bearer --------------->   " +  token_no_bearer);



        JWTUtil.verifyToken(token_no_bearer, "jwt_secret");


      //  System.out.println( "------------AuthorizeFilter--------------" + access_jwt_token );

        //ServerHttpRequest mutableReq = serverHttpRequest.mutate().header("userId", userId).build();

        // ServerWebExchange mutableExchange = exchange.mutate().request(mutableReq).build();

        return chain.filter(exchange);


    }


    @Override
    public int getOrder() {
        return -100;
    }
}
>>>>>>> 655e65f80ec2fd6d62e4c37dd39edf3991b7670f
